package com.bocai.kelala.ui.travel.set.act;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.kelala.App;
import com.bocai.kelala.R;
import com.bocai.kelala.base.BaseActivity;
import com.bocai.kelala.bean.AddressListBean;
import com.bocai.kelala.ui.travel.main.act.MainActivity;
import com.bocai.kelala.ui.travel.set.ada.AddressAda;
import com.bocai.kelala.ui.travel.set.contract.AddAddressContract;
import com.bocai.kelala.ui.travel.set.module.AddAddressModule;
import com.bocai.kelala.ui.travel.set.presenter.AddAddressPresenter;
import com.bocai.mybase.base.BaseRecyclerAdapter;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by yuanfei on 2017/6/12.
 */

public class AddressActivity extends BaseActivity implements AddAddressContract.view,XRecyclerView.LoadingListener {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.right_btn)
    Button rightBtn;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.recycle_address)
    XRecyclerView recycleAddress;
    @BindView(R.id.btn_add_address)
    Button btnAddAddress;
    AddressAda addressAda;
    @Inject
    AddAddressPresenter presenter;
    int index = 0;

    @Override
    public int getLayoutId() {
        return R.layout.activity_address;
    }

    @Override
    public void initPresenter() {
        App.get(this).getAppComponent().addSub(new AddAddressModule(this)).inject(this);
    }

    @Override
    public void initView() {
        initToolbar("地址管理");
        initBack(imgBack,llBack);
        index = getIntent().getIntExtra("index",0);
        recycleAddress.setLayoutManager(new LinearLayoutManager(this));
        recycleAddress.setLoadingListener(this);

        addressAda = new AddressAda();
        recycleAddress.setAdapter(addressAda);
        recycleAddress.refresh();
        addressAda.setOnItemOnListener(new BaseRecyclerAdapter.OnItemClickedListener() {
            @Override
            public void onItemClicked(int id, int index) {
                Intent intent = new Intent();
                intent.putExtra("data",addressAda.getDataList().get(index));
                setResult(333,intent);
                finish();
            }
        });
    }
    @OnClick({R.id.btn_add_address})
    public void OnClick(View view){
        switch (view.getId()){
            case R.id.btn_add_address:
                startActivity(AddAddressActivity.class);
                break;
        }
    }

    @Override
    public void addAddressSuccess(AddressListBean.AddressesBean bean) {

    }

    @Override
    public void getAddressList(AddressListBean bean) {
        recycleAddress.refreshComplete();
        if (bean!=null) {
            addressAda.resetItems(bean.getAddresses());
        }
        recycleAddress.setLoadingMoreEnabled(false);
    }

    @Override
    public void deleteAddressSuccess() {

    }

    @Override
    public void modifyAddress() {

    }

    @Override
    public void showLoading(String title) {
        startProgressDialog(title);
    }

    @Override
    public void stopLoading() {
        stopProgressDialog();
    }

    @Override
    public void showErrorTip(String msg) {
        recycleAddress.refreshComplete();

        if (msg.equals("用户授权失败")){
            msg = "您的登录已失效，请重新登录";
            startActivity(new Intent(this,MainActivity.class).putExtra("index",1));
            finish();
        }
        showShortToast(msg);
    }

    @Override
    public void onRefresh() {
        presenter.addressList();
    }

    @Override
    public void onLoadMore() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        recycleAddress.refresh();
    }
}
